package com.lon.over.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lon.over.R;
import com.lon.over.base.BaseFragment;
import com.lon.over.ui.fragment.DiscoverFragment;
import com.lon.over.ui.fragment.HomeFragment;
import com.lon.over.ui.fragment.MeFragment;
import com.lon.over.ui.fragment.RecommendFragment;
import com.lon.over.utils.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends AppCompatActivity {

    private HomeFragment mHomeFragment;
    private RecommendFragment mRecommendFragment;
    private DiscoverFragment mDiscoverFragment;
    private MeFragment mMeFragment;
    @BindView(R.id.main_navigation)
    public BottomNavigationView mNavigationView;
    private FragmentManager mFm;
    private Unbinder mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBind = ButterKnife.bind(this);
        initFragment();
        initListener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind.unbind();
        }
    }

    private void initListener() {
        mNavigationView.setOnNavigationItemSelectedListener(item -> {
            if (item.getItemId() == R.id.home) {
                LogUtils.d(this, "name --  > " + item.getTitle());
                switchFragment(mHomeFragment);
            } else if (item.getItemId() == R.id.recommend) {
                LogUtils.i(this, "name -- >" + item.getTitle());
                switchFragment(mRecommendFragment);
            } else if (item.getItemId() == R.id.discover) {
                LogUtils.w(this, "name -- >" + item.getTitle());
                switchFragment(mDiscoverFragment);
            } else if (item.getItemId() == R.id.mine) {
                LogUtils.e(this, "name -- >" + item.getTitle());
                switchFragment(mMeFragment);
            }
            return true;
        });
    }

    private void switchFragment(BaseFragment fragment) {
        FragmentTransaction transaction = mFm.beginTransaction();
        transaction.replace(R.id.view_main_container,fragment);
        transaction.commit();
    }

    private void initFragment() {
        mMeFragment = new MeFragment();
        mHomeFragment = new HomeFragment();
        mDiscoverFragment = new DiscoverFragment();
        mRecommendFragment = new RecommendFragment();
        mFm = getSupportFragmentManager();
        switchFragment(mHomeFragment);
    }

}
